package org.ece3574.WTParty;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class WebServiceClient {
	
	final static String serverURL = "http://wtparty.overstrom.net/WTPWebSrv/TestServer?";
	private static String getHttp(String URL)
	{
		HttpClient hc = new DefaultHttpClient();
		HttpGet request = new HttpGet(URL);

		try
		{
			HttpResponse rp = hc.execute(request);
			if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
			{
				BufferedReader dataReader = new BufferedReader(new InputStreamReader(rp.getEntity().getContent()));
				StringBuffer sb = new StringBuffer();
				String line;
				while((line=dataReader.readLine())!=null)
				{
					sb.append(line);
				}
				dataReader.close();
				return sb.toString();
			}
			else
			{
				Log.d("Error", "Error with request: " + URL + "Server returned " + rp.getStatusLine().getStatusCode());
			}
		}
		catch(IOException ex)
		{
			Log.d("Error", "Error with request: " + URL, ex);
		}
		return null;

	}
	
	public static GeoLocation geoCode(String input)
	{
		String reqString = serverURL + "command=geocode&input=" + URLEncoder.encode(input);
		String locString = getHttp(reqString);
		GeoLocation geoLoc = GeoLocation.getFromString(locString);
		return geoLoc;
	}
	
	

}
